import os
import json

class Path():
    '''
    初始化时获得本文件的绝对路径
    back（返回上级方式）:
    '''

    def __init__(self):
        fp = os.path.abspath(__file__)
        ul_pos = fp[::-1].find('\\')
        self.fp = fp[:len(fp)-ul_pos]
        # print(self.fp)

    def back(self, n=1):
        if n == 0:
            fp = self.fp
            ul_pos = fp.find('\\')
            self.fp = fp[:ul_pos+1]
        else:
            for i in range(n):
                fp = self.fp
                ul_pos = fp[-2::-1].find('\\')
                self.fp = fp[:len(fp) - ul_pos-1]
        return self.fp

    def isHere(self, filename):
        fp = self.fp + filename
        # print(fp) # 查看当前文件路径
        if not os.path.exists(fp):
            os.mkdir(fp)
            print('>新建文件夹：'+filename)
        self.fp = fp+'\\'
        return self.fp

    def new_file(self, filename, data):
        with open(self.fp+filename, 'w+', encoding='utf-8') as ff:
            ff.write(data)

    def now(self):
        return os.listdir(self.fp)


class PathGod(Path):
    def ini(self):
        self.fp_TEST = self.isHere('TEST')
        self.fp_Asset = self.isHere('Asset')
        self.back()

        self.fp_Head = self.isHere('Head')
        self.new_file('Page.py', data['Page'])
        self.new_file('数据读写.py', data['dtR'])
        self.back()

        self.fp_Case = self.isHere('Case')
        self.new_file('test.py', data['test'])
        self.back()

        self.fp_Data = self.isHere('Data')
        self.new_file('data.py', data['data'])
        self.back()

        self.fp_FuncLogic = self.isHere('FuncLogic')
        self.new_file('func.py', data['func'])
        self.back()

        self.fp_Report = self.isHere('Report')
        self.new_file('测试报告生成.py', data['report'])
        self.new_file('邮件.py', data['mail'])
        self.fp_log = self.isHere('log')
        self.back()
        self.back()

        return self.fp

    def down(self):
        lis = [x for x in dir(a) if 'fp' in x]
        dic = {}
        for i in lis:
            dic[i] = self.i
        dic['head'] = self.fp_head
        dic['path'] = self.fp
        dit = json.dumps(dic, sort_keys=False, indent=4, separators=(',', ': '))
        with open('path.json', 'w', encoding='utf-8') as ff:
            ff.write(dit)

    def inin(self):
        with open('path.json', 'r', encoding='utf-8') as ff:
            dic = json.load(ff)

class Data():

    def __init__(self):
        self.data = {}

    # 从py文件读取代码
    def data_on(self, fileway, filename):
        with open(fileway, 'r', encoding='utf-8') as ff:
            self.data[filename] = ff.read()

    def data_on_ON(self):
        self.data_on(PATH + r'TEST\Head\Page.py', 'Page')
        self.data_on(PATH + r'TEST\Head\数据读写.py', 'dtR')
        self.data_on(PATH + r'TEST\FuncLogic\func.py', 'func')
        self.data_on(PATH + r'TEST\Data\data.py', 'data')
        self.data_on(PATH + r'TEST\Case\test.py', 'test')
        self.data_on(PATH + r'TEST\Report\测试报告生成.py', 'report')
        self.data_on(PATH + r'TEST\Report\邮件.py', 'mail')

    # 书写为json格式保存
    def data_down(self):
        d = json.dumps(self.data, sort_keys=False, indent=4, separators=(',', ': '))
        with open('data.json', 'w+', encoding='utf-8') as ff:
            ff.write(d)

    # 从json格式读出
    def data_up(self):
        with open('data.json', 'r', encoding='utf-8') as ff:
            self.data = json.load(ff)
        return self.data

    # 从内部装填
    def data_ready(self, data):
        self.data = data

PATH = Path().fp

# d = Data()
# d.data_on_ON()
# d.data_down()

data = Data().data_up()
PathGod().ini()